local function refresh()
	local cjson = require "cjson"
	local realip = ngx.var.arg_realip;
	if(realip == nil) then 
		ngx.say(cjson.encode({ code=405,msg="参数realip为空" }))
	    return 
	end
	local redis = require "redisutil"
    

    local curProxyInfo = {};--当前路由信息
	if(redis.testEnable() == nil) then
	  ngx.say(cjson.encode({ code=405,msg="统一网关api redis 服务不可用，不能提供服务" }))
	  return 
	end
	local res = redis.redkey(realip);
	--ngx.say(realip)
	--ngx.say(cjson.encode(res))
	if res == nil then
	  local resp = {code=405,mgs="应用未配置服务器信息，不能访问"}
	  ngx.say(cjson.encode(resp))
	  return 
	else
	  curProxyInfo = cjson.decode(res);
	end

	if(curProxyInfo.tokenInfo) then
		local TOKEN_API_REL_URL = "/api/openapi/v1/auth/refreshToken"

		curProxyInfo.powerCode = curProxyInfo.powerCode or "default"
		local ekbargs = {powerCode = curProxyInfo.powerCode, accessToken = curProxyInfo.tokenInfo.accessToken,refreshToken=curProxyInfo.tokenInfo.refreshToken}
		local TOKEN_API_ARG = "?" .. ngx.encode_args(ekbargs)
		local powerEntity = {powerCode = curProxyInfo.powerCode,accessToken=curProxyInfo.tokenInfo.accessToken,refreshToken=curProxyInfo.tokenInfo.refreshToken}
		--ngx.say(ngx.encode_args(powerEntity))
		--ngx.say(curProxyInfo.urlPrefix .. TOKEN_API_REL_URL .. TOKEN_API_ARG )

		local res = ngx.location.capture("/ekbauthproxy",
		 { method=ngx.HTTP_POST,
		   vars = { urlPrefix=curProxyInfo.urlPrefix,
		      relativeUrl=TOKEN_API_REL_URL ,
		      arg=TOKEN_API_ARG
		   },
               body = ngx.encode_args(powerEntity)
		 }
		);
		local ok , tokendata = pcall(cjson.decode,res.body)

		if not ok then
			ngx.say(res.body)
			ngx.say(cjson.encode({ code=405,msg="刷新token失败" }))
			return 
		else
			curProxyInfo.tokenInfo = tokendata.value;
			redis.setvalue(realip,cjson.encode(curProxyInfo))
			local resp = {code=200,msg="刷新token 成功"}
			ngx.say(cjson.encode(resp))
		--ngx.say(cjson.encode(tokendata.value))
		end
		--,body = ngx.encode_args(powerEntity)
	else
		local resp = {code=405,mgs="应用token信息不存在，不能刷新"}
	  	ngx.say(cjson.encode(resp))
	  	return 
	end
	

end

refresh();